package com.ls.recursive;

/**
 * @program: dataStructure
 * @author: lishun
 * @create: 2021-12-14 16:21
 **/
import lombok.Data;

/**
 * 八皇后问题
 * @author liushun
 */
@Data
public class EightQueens {
    private int max = 8;
    private int[] arr =  new int[max];
    /**
     * 解法次数
     */
    private int count;

    public static void main(String[] args) {
        EightQueens queens = new EightQueens();
        queens.put(0);
        System.out.println(queens.count);
    }

    /**
     * 检验第n个皇后是否与前面冲突
     */
    public boolean check(int n){
        for (int i = 0;i < n;i++){
            // 判断是否处于同一列
            boolean flag1 = arr[i]==arr[n];
            // 判断是否处于同意对角线(根据斜率原理)
            boolean flag2 = Math.abs(n-i)==Math.abs(arr[n]-arr[i]);
            if (flag1||flag2){
                return false;
            }
        }
        return true;
    }

    /**
     * 摆放皇后
     */
    public void put(int n){
        if (n==max){
            this.print();
            this.count++;
            return;
        }
        for (int i = 0;i < max;i++ ){
            arr[n] = i;
            if (check(n)){
                put(n+1);
            }
        }
    }


    /**
     * 显示皇后白发
     */
    public void print(){
        for (int i = 0; i < this.arr.length; i++) {
            System.out.print(arr[i]);
        }
        System.out.println();
    }





}
